package ru.vsu.cs.pm.models;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * User: ohodyakov
 * Date: 04.01.2011
 * Time: 12:02:53
 */
@Entity
@Table(name="dealership")
@NamedQueries({
        @NamedQuery(name = "dealerShip.get.by.item.id",
                query = "select d from DealerShip d left join fetch d.accountFrom " +
                        "left join fetch d.accountTo " +
                        "where d.item.id = :itemId"),

        @NamedQuery(name = "dealerShip.get.count.by.item.id",
                query = "select count(d) from DealerShip d " +
                        "where d.item.id = :itemId"),

        @NamedQuery(name = "dealerShip.get.by.user.id.and.by.item.type",
                query = "select d from DealerShip d left join fetch d.accountFrom " +
                        "left join fetch d.accountTo " +
                        "where d.item.user.id = :userId and " +
                        "d.item.type in (:types)"),

        @NamedQuery(name = "dealerShip.get.count.by.user.id.and.item.type",
                query = "select count(d) from DealerShip d " +
                        "where d.item.user.id = :userId and " +
                        "d.item.type in (:types)")
})
public class DealerShip implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "account_from", nullable = true)
    private Account accountFrom;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "account_to", nullable = true)
    private Account accountTo;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "article_id", nullable = false)
    private Item item;

    @Column(name = "money", nullable = false)
    private Long money;

    @Column(name = "description", nullable = true)
    private String description;

    @Temporal(TemporalType.DATE)
	@Column(name = "date_start", nullable = false)
    private Date startDate;

    @Temporal(TemporalType.DATE)
	@Column(name = "date_end", nullable = false)
    private Date endDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Account getAccountFrom() {
        return accountFrom;
    }

    public void setAccountFrom(Account accountFrom) {
        this.accountFrom = accountFrom;
    }

    public Account getAccountTo() {
        return accountTo;
    }

    public void setAccountTo(Account accountTo) {
        this.accountTo = accountTo;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Long getMoney() {
        return money;
    }

    public void setMoney(Long money) {
        this.money = money;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
}
